import request from '@/utils/request'

// Zabbix服务器管理
export const monitoringApi = {
  // 获取监控概览
  getOverview() {
    return request({
      url: '/api/monitoring/dashboard/overview/',
      method: 'get'
    })
  },

  // Zabbix服务器管理
  getZabbixServers() {
    return request({
      url: '/api/monitoring/zabbix-servers/',
      method: 'get'
    })
  },

  createZabbixServer(data: any) {
    return request({
      url: '/api/monitoring/zabbix-servers/',
      method: 'post',
      data
    })
  },

  updateZabbixServer(id: number, data: any) {
    return request({
      url: `/api/monitoring/zabbix-servers/${id}/`,
      method: 'put',
      data
    })
  },

  deleteZabbixServer(id: number) {
    return request({
      url: `/api/monitoring/zabbix-servers/${id}/`,
      method: 'delete'
    })
  },

  testZabbixConnection(id: number) {
    return request({
      url: `/api/monitoring/zabbix-servers/${id}/test-connection/`,
      method: 'post'
    })
  },

  // 主机组管理
  getHostGroups() {
    return request({
      url: '/api/monitoring/host-groups/',
      method: 'get'
    })
  },

  // 根据主机组获取主机
  getHostsByGroup(groupId: string) {
    return request({
      url: `/api/monitoring/host-groups/${groupId}/hosts/`,
      method: 'get'
    })
  },



  // Grafana服务器管理
  getGrafanaServers() {
    return request({
      url: '/api/monitoring/grafana-servers/',
      method: 'get'
    })
  },

  createGrafanaServer(data: any) {
    return request({
      url: '/api/monitoring/grafana-servers/',
      method: 'post',
      data
    })
  },

  updateGrafanaServer(id: number, data: any) {
    return request({
      url: `/api/monitoring/grafana-servers/${id}/`,
      method: 'put',
      data
    })
  },

  deleteGrafanaServer(id: number) {
    return request({
      url: `/api/monitoring/grafana-servers/${id}/`,
      method: 'delete'
    })
  },

  testGrafanaConnection(id: number) {
    return request({
      url: `/api/monitoring/grafana-servers/${id}/test-connection/`,
      method: 'post'
    })
  },

  // 监控主机管理
  getMonitoringHosts() {
    return request({
      url: '/api/monitoring/monitoring-hosts/',
      method: 'get'
    })
  },

  createMonitoringHost(data: any) {
    return request({
      url: '/api/monitoring/monitoring-hosts/',
      method: 'post',
      data
    })
  },

  updateMonitoringHost(id: number, data: any) {
    return request({
      url: `/api/monitoring/monitoring-hosts/${id}/`,
      method: 'put',
      data
    })
  },

  deleteMonitoringHost(id: number) {
    return request({
      url: `/api/monitoring/monitoring-hosts/${id}/`,
      method: 'delete'
    })
  },

  // 监控告警管理
  getAlerts(params?: any) {
    return request({
      url: '/api/monitoring/alerts/',
      method: 'get',
      params
    })
  },

  acknowledgeAlert(id: number, data: any) {
    return request({
      url: `/api/monitoring/alerts/${id}/acknowledge/`,
      method: 'post',
      data
    })
  },

  // 告警规则管理
  getAlertRules(params?: any) {
    return request({
      url: '/api/monitoring/alert-rules/',
      method: 'get',
      params
    })
  },

  createAlertRule(data: any) {
    return request({
      url: '/api/monitoring/alert-rules/',
      method: 'post',
      data
    })
  },

  updateAlertRule(id: number, data: any) {
    return request({
      url: `/api/monitoring/alert-rules/${id}/`,
      method: 'put',
      data
    })
  },

  deleteAlertRule(id: number) {
    return request({
      url: `/api/monitoring/alert-rules/${id}/`,
      method: 'delete'
    })
  },

  toggleAlertRule(id: number) {
    return request({
      url: `/api/monitoring/alert-rules/${id}/toggle_active/`,
      method: 'post'
    })
  },

  testAlertRule(id: number) {
    return request({
      url: `/api/monitoring/alert-rules/${id}/test_rule/`,
      method: 'post'
    })
  },

  // 同步操作
  syncHostsToZabbix(data: any) {
    return request({
      url: '/api/monitoring/sync/hosts-to-zabbix/',
      method: 'post',
      data
    })
  },

  // 快速关联主机
  quickAssociateHost(data: any) {
    return request({
      url: '/api/monitoring/quick-associate/',
      method: 'post',
      data
    })
  },

  // 清理监控主机数据
  cleanupMonitoringHosts(data: any) {
    return request({
      url: '/api/monitoring/cleanup/monitoring-hosts/',
      method: 'post',
      data
    })
  },

  // 同步告警数据
  syncAlertsFromZabbix(data: any) {
    return request({
      url: '/api/monitoring/sync/alerts-from-zabbix/',
      method: 'post',
      data
    })
  },

  syncAlertsFromZabbix(data: any) {
    return request({
      url: '/api/monitoring/sync/alerts-from-zabbix/',
      method: 'post',
      data
    })
  },

  syncMonitoringStatusToCMDB(data: any) {
    return request({
      url: '/api/monitoring/sync/monitoring-status-to-cmdb/',
      method: 'post',
      data
    })
  },

  resolveAlert(data: any) {
    return request({
      url: '/api/monitoring/resolve-alert/',
      method: 'post',
      data
    })
  },

  deleteAlert(alertId: number) {
    return request({
      url: `/api/monitoring/alerts/${alertId}/`,
      method: 'delete'
    })
  },

  // 主机监控状态
  getHostStatus(zabbixServerId?: string, params?: any) {
    const requestParams = { ...params }
    if (zabbixServerId) {
      requestParams.zabbix_server_id = zabbixServerId
    }
    return request({
      url: '/api/monitoring/dashboard/host-status/',
      method: 'get',
      params: requestParams
    })
  },

  // 主机监控状态
  getHostMonitoringStatus() {
    return request({
      url: '/api/monitoring/dashboard/host-status/',
      method: 'get'
    })
  },

  // 获取主机详细监控指标
  getHostMetrics(hostId: string, zabbixServerId?: string) {
    const params: any = {}
    if (zabbixServerId) {
      params.zabbix_server_id = zabbixServerId
    }
    return request({
      url: `/api/monitoring/hosts/${hostId}/metrics/`,
      method: 'get',
      params
    })
  },

  // 获取主机配置信息
  getHostConfiguration(hostId: string, zabbixServerId?: string) {
    const params: any = {}
    if (zabbixServerId) {
      params.zabbix_server_id = zabbixServerId
    }
    return request({
      url: `/api/monitoring/hosts/${hostId}/configuration/`,
      method: 'get',
      params
    })
  },

  // 更新主机配置
  updateHostConfiguration(hostId: string, config: any, zabbixServerId?: string) {
    const data = { ...config }
    if (zabbixServerId) {
      data.zabbix_server_id = zabbixServerId
    }
    return request({
      url: `/api/monitoring/hosts/${hostId}/configuration/`,
      method: 'put',
      data
    })
  },

  // 获取可用模板
  getAvailableTemplates(zabbixServerId?: string) {
    const params: any = {}
    if (zabbixServerId) {
      params.zabbix_server_id = zabbixServerId
    }
    return request({
      url: '/api/monitoring/templates/',
      method: 'get',
      params
    })
  },

  // 获取可用主机组
  getAvailableHostGroups(zabbixServerId?: string) {
    const params: any = {}
    if (zabbixServerId) {
      params.zabbix_server_id = zabbixServerId
    }
    return request({
      url: '/api/monitoring/hostgroups/',
      method: 'get',
      params
    })
  },

  // 获取主机监控项
  getHostItems(hostId: string, zabbixServerId?: string, page?: number, pageSize?: number, search?: string) {
    const params: any = { host_id: hostId }
    if (zabbixServerId) {
      params.zabbix_server_id = zabbixServerId
    }
    if (page) {
      params.page = page
    }
    if (pageSize) {
      params.page_size = pageSize
    }
    if (search) {
      params.search = search
    }
    return request({
      url: '/api/monitoring/hosts/items/',
      method: 'get',
      params
    })
  },

  // 更新监控项状态
  updateItemStatus(itemId: string, status: number, zabbixServerId?: string) {
    const data: any = {
      item_id: itemId,
      status: status
    }
    if (zabbixServerId) {
      data.zabbix_server_id = zabbixServerId
    }
    return request({
      url: '/api/monitoring/items/update-status/',
      method: 'post',
      data
    })
  },

  // 删除监控项
  deleteItem(itemId: string, zabbixServerId?: string) {
    const data: any = { item_id: itemId }
    if (zabbixServerId) {
      data.zabbix_server_id = zabbixServerId
    }
    return request({
      url: '/api/monitoring/items/delete/',
      method: 'post',
      data
    })
  },

  // Zabbix仪表板API
  getDashboardHostGroups(params?: any) {
    return request({
      url: '/api/monitoring/dashboard/host-groups/',
      method: 'get',
      params
    })
  },

  getDashboardHosts(params: any) {
    return request({
      url: '/api/monitoring/dashboard/hosts/',
      method: 'get',
      params
    })
  },

  getDashboardGraphs(params: any) {
    return request({
      url: '/api/monitoring/dashboard/graphs/',
      method: 'get',
      params
    })
  },

  getDashboardGraphData(params: any) {
    return request({
      url: '/api/monitoring/dashboard/graph-data/',
      method: 'get',
      params
    })
  },

  createDashboardGraph(data: any) {
    return request({
      url: '/api/monitoring/dashboard/create-graph/',
      method: 'post',
      data
    })
  },

  getDashboardHostItems(params: any) {
    return request({
      url: '/api/monitoring/dashboard/host-items/',
      method: 'get',
      params
    })
  },

  // 邮件配置管理
  getEmailConfigs() {
    return request({
      url: '/api/monitoring/email-configs/',
      method: 'get'
    })
  },

  createEmailConfig(data: any) {
    return request({
      url: '/api/monitoring/email-configs/',
      method: 'post',
      data
    })
  },

  updateEmailConfig(id: number, data: any) {
    return request({
      url: `/api/monitoring/email-configs/${id}/`,
      method: 'patch',
      data
    })
  },

  deleteEmailConfig(id: number) {
    return request({
      url: `/api/monitoring/email-configs/${id}/`,
      method: 'delete'
    })
  },

  testEmailConfig(id: number) {
    return request({
      url: `/api/monitoring/email-configs/${id}/test_config/`,
      method: 'post'
    })
  },

  setDefaultEmailConfig(id: number) {
    return request({
      url: `/api/monitoring/email-configs/${id}/set_default/`,
      method: 'post'
    })
  },

  // 通知渠道管理
  getNotificationChannels() {
    return request({
      url: '/api/monitoring/notification-channels/',
      method: 'get'
    })
  },

  createNotificationChannel(data: any) {
    return request({
      url: '/api/monitoring/notification-channels/',
      method: 'post',
      data
    })
  },

  updateNotificationChannel(id: number, data: any) {
    return request({
      url: `/api/monitoring/notification-channels/${id}/`,
      method: 'patch',
      data
    })
  },

  deleteNotificationChannel(id: number) {
    return request({
      url: `/api/monitoring/notification-channels/${id}/`,
      method: 'delete'
    })
  },

  testNotificationChannel(id: number) {
    return request({
      url: `/api/monitoring/notification-channels/${id}/test_channel/`,
      method: 'post'
    })
  },

  // 搜索监控模板
  searchTemplates(zabbixServerId: string, searchQuery: string) {
    return request({
      url: '/api/monitoring/templates/search/',
      method: 'get',
      params: {
        zabbix_server_id: zabbixServerId,
        search: searchQuery
      }
    })
  },

  // 获取模板监控项
  getTemplateItems(zabbixServerId: string, templateId: string, searchQuery?: string) {
    const params: any = {
      zabbix_server_id: zabbixServerId,
      template_id: templateId
    }
    if (searchQuery) {
      params.search = searchQuery
    }
    return request({
      url: '/api/monitoring/template-items/',
      method: 'get',
      params
    })
  },

  // 获取Zabbix模板列表
  getZabbixTemplates(zabbixServerId: number) {
    return request({
      url: `/api/monitoring/zabbix-servers/${zabbixServerId}/templates/`,
      method: 'get'
    })
  },

  // 获取模板详情
  getZabbixTemplateDetail(zabbixServerId: number, templateId: string) {
    return request({
      url: `/api/monitoring/zabbix-servers/${zabbixServerId}/templates/${templateId}/`,
      method: 'get'
    })
  },



  // 批量导出模板
  exportZabbixTemplates(zabbixServerId: number, templateIds: string[]) {
    return request({
      url: `/api/monitoring/zabbix-servers/${zabbixServerId}/batch-export-templates/`,
      method: 'post',
      data: { template_ids: templateIds },
      responseType: 'blob'  // 重要：指定响应类型为blob以处理ZIP文件
    })
  },

  // 更新模板
  updateZabbixTemplate(zabbixServerId: number, templateId: string, data: any) {
    return request({
      url: `/api/monitoring/zabbix-servers/${zabbixServerId}/update-template/${templateId}/`,
      method: 'put',
      data
    })
  },

  // 克隆模板
  cloneZabbixTemplate(zabbixServerId: number, templateId: string, data: any) {
    return request({
      url: `/api/monitoring/zabbix-servers/${zabbixServerId}/clone-template/${templateId}/`,
      method: 'post',
      data
    })
  },

  // 删除模板
  deleteZabbixTemplate(zabbixServerId: number, templateId: string) {
    return request({
      url: `/api/monitoring/zabbix-servers/${zabbixServerId}/delete-template/${templateId}/`,
      method: 'delete'
    })
  },

  // 创建模板
  createZabbixTemplate(zabbixServerId: number, data: any) {
    return request({
      url: `/api/monitoring/zabbix-servers/${zabbixServerId}/create-template/`,
      method: 'post',
      data
    })
  },

  // 获取Zabbix监控项列表
  getZabbixItems(zabbixServerId: number) {
    return request({
      url: `/api/monitoring/zabbix-servers/${zabbixServerId}/items/`,
      method: 'get'
    })
  },

  // 获取Zabbix主机列表
  getZabbixHosts(zabbixServerId: number) {
    return request({
      url: `/api/monitoring/dashboard/host-status/`,
      method: 'get',
      params: {
        zabbix_server_id: zabbixServerId
      }
    })
  },

  // 导出模板
  exportZabbixTemplate(zabbixServerId: number, templateId: string) {
    return request({
      url: `/api/monitoring/zabbix-servers/${zabbixServerId}/export-template/${templateId}/`,
      method: 'get'
    })
  },

  // 导入模板
  importZabbixTemplate(zabbixServerId: number, formData: FormData) {
    return request({
      url: `/api/monitoring/zabbix-servers/${zabbixServerId}/import-template/`,
      method: 'post',
      data: formData
      // 不设置headers，让请求拦截器和浏览器自动处理FormData
    })
  },

  // 批量导入模板（ZIP文件）
  batchImportZabbixTemplates(zabbixServerId: number, formData: FormData) {
    return request({
      url: `/api/monitoring/zabbix-servers/${zabbixServerId}/batch-import-templates/`,
      method: 'post',
      data: formData
      // 不设置headers，让请求拦截器和浏览器自动处理FormData
    })
  },

  // 获取监控项详情
  getZabbixItemDetail(zabbixServerId: number, itemId: string) {
    return request({
      url: `/api/monitoring/zabbix-servers/${zabbixServerId}/items/${itemId}/`,
      method: 'get'
    })
  },

  // 创建监控项
  createZabbixItem(zabbixServerId: number, data: any) {
    return request({
      url: `/api/monitoring/zabbix-servers/${zabbixServerId}/create_item/`,
      method: 'post',
      data
    })
  },

  // 更新监控项
  updateZabbixItem(zabbixServerId: number, itemId: string, data: any) {
    return request({
      url: `/api/monitoring/zabbix-servers/${zabbixServerId}/update_item/`,
      method: 'put',
      data: { ...data, itemid: itemId }
    })
  },

  // 删除监控项
  deleteZabbixItem(zabbixServerId: number, itemId: string) {
    return request({
      url: `/api/monitoring/zabbix-servers/${zabbixServerId}/delete_item_action/`,
      method: 'delete',
      data: { itemid: itemId }
    })
  },

  // 测试监控项
  testZabbixItem(zabbixServerId: number, itemId: string) {
    return request({
      url: `/api/monitoring/zabbix-servers/${zabbixServerId}/test_item/`,
      method: 'post',
      data: { itemid: itemId }
    })
  },

  // 批量删除监控项
  batchDeleteZabbixItems(zabbixServerId: number, itemIds: string[]) {
    return request({
      url: `/api/monitoring/zabbix-servers/${zabbixServerId}/batch-delete-items/`,
      method: 'post',
      data: { item_ids: itemIds }
    })
  },

  // 获取监控项历史数据
  getItemHistory(zabbixServerId: number, hostName: string, itemName: string, itemKey?: string, timeRange: number = 3600, limit: number = 100) {
    const params: any = {
      host_name: hostName,
      item_name: itemName,
      time_range: timeRange,
      limit
    }
    if (itemKey) {
      params.item_key = itemKey
    }
    return request({
      url: `/api/monitoring/zabbix-servers/${zabbixServerId}/get_item_history/`,
      method: 'get',
      params
    })
  },

  // SSE告警推送测试
  testAlertPush(data: any) {
    return request({
      url: '/api/monitoring/test-alert-push/',
      method: 'post',
      data
    })
  },

  // SSE通知渠道配置
  getSSEConfig() {
    return request({
      url: '/api/monitoring/sse/config/',
      method: 'get'
    })
  },

  saveSSEConfig(data: any) {
    return request({
      url: '/api/monitoring/sse/config/',
      method: 'post',
      data
    })
  }

}
